package cn.blnp.net.flowable.boot.ext.cmd;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.flowable.common.engine.impl.interceptor.Command;
import org.flowable.common.engine.impl.interceptor.CommandContext;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.impl.util.CommandContextUtil;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.engine.runtime.ProcessInstanceBuilder;

import java.util.Map;

/**
 * <h3>自定义流程重建命令实现</h3>
 *
 * @author <a href="mailto:blnp.yibin@qq.com">lyb</a>
 * @version 1.0
 * @since 2025/6/19 10:35
 */
@Slf4j
@AllArgsConstructor
public class ReCreateProcessInstanceCmd implements Command<ProcessInstance> {

    protected Map<String, Object> procInstMap;

    @Override
    public ProcessInstance execute(CommandContext commandContext) {
        RuntimeService runtimeService = CommandContextUtil
                .getProcessEngineConfiguration(commandContext)
                .getRuntimeService();
        //重建流程
        ProcessInstanceBuilder processInstanceBuilder = runtimeService.createProcessInstanceBuilder();
        ProcessInstance newProcessInstance = processInstanceBuilder
                .processDefinitionId((String)procInstMap.get("processDefinitionId"))
                .processDefinitionKey((String)procInstMap.get("processDefinitionKey"))
                .predefineProcessInstanceId((String)procInstMap.get("processInstanceId"))
                .businessKey((String)procInstMap.get("businessKey"))
                .variables((Map)procInstMap.get("variables"))
                .tenantId((String)procInstMap.get("tenantId"))
                .start();
        return newProcessInstance;
    }
}
